<template>
  <BasicCard>
    <BasicVxeTable v-bind="gridOptions"> </BasicVxeTable>
  </BasicCard>
</template>

<script setup lang="ts">
  import dayjs from 'dayjs'
  import { columnsOutbound } from '../data'
  import { getChaXunChuRuKuPingZhong, postChuRuKuJiLuChaXun } from '@/api/wms'

  const props = defineProps({
    type: {
      type: String,
      default: 'out'
    }
  })

  const { t } = useI18n()

  const gridOptions = reactive<BasicGridProps>({
    extraConfig: {
      title: props.type === 'out' ? t('custom.出库记录') : t('custom.入库记录'),
      isCustom: false,
      isExport: false,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `kucunguanli-chengpinguanli-jilu-${props.type}`, // id用于缓存,务必配置
    maxHeight: '100%',
    loading: true,
    showFooter: true,
    columns: columnsOutbound,
    toolbarConfig: {}, // 工具栏
    footerMethod({ columns, data }) {
      return footerMothodFunc(columns, data)
    }
  })

  const pingZhongList = ref<any>([])
  const pingZhongArr = ref<any>([])
  const timeVals = ref<any>([
    dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss'),
    dayjs().format('YYYY-MM-DD HH:mm:ss')
  ])

  const getList = () => {
    postChuRuKuJiLuChaXun({
      inOutType: props.type === 'out' ? 1 : 2,
      materialCode: pingZhongArr.value,
      begin: dayjs(timeVals.value[0]).format('YYYY-MM-DD HH:mm:ss'),
      end: dayjs(timeVals.value[1]).format('YYYY-MM-DD') + ' 23:59:59'
    })
      .then((res: any) => {
        gridOptions.loading = false
        gridOptions.data = res.detail.map((value) => {
          return {
            ...value,
            timeStamp: dayjs(value.timeStamp).format('YYYY-MM-DD HH:mm:ss')
          }
        })
      })
      .catch(() => {
        gridOptions.loading = false
      })
  }

  const footerMothodFunc = (columns, data) => {
    return [
      columns.map((column, columnIndex) => {
        if (columnIndex === 0) {
          return '合计'
        } else if (columnIndex === 3) {
          return handleFunc(data, 0, 'packCount')
        } else if (columnIndex === 4) {
          return handleFunc(data, 2, 'weight')
        } else {
          return '--'
        }
      })
    ]
  }

  const handleFunc = (data, num, key) => {
    let count = 0
    data.forEach((item) => {
      count += item[key]
    })
    return count.toFixed(num)
  }

  // 获取品种
  const getVariety = () => {
    getChaXunChuRuKuPingZhong({
      inOutType: props.type === 'out' ? 1 : 2,
      begin: dayjs(timeVals.value[0]).format('YYYY-MM-DD HH:mm:ss'),
      end: dayjs(timeVals.value[1]).format('YYYY-MM-DD') + ' 23:59:59'
    }).then((res: any) => {
      pingZhongList.value = res.map((value) => {
        return {
          name: value.materialsCode,
          id: value.materialsCode
        }
      })
      pingZhongArr.value = res.map((value) => {
        return value.materialsCode
      })
      getList()
    })
  }

  onMounted(() => {
    getVariety()
  })
</script>
